<?php
namespace Poi\EttsBundle\Model;

/**
 * @author Ricardo J. Vazquez
 * 
 * Representation of a Vehicle object from the database
 */
class Vehicle extends Model
{
	public $VIN = '';
	public $RfidKey = '';
	public $LicensePlate = '';
	public $Owner = '';
	public $Expiration = '';
	public $VehicleTag = '';
	public $LoadCapacity = 0.0;
	public $HorsePower = 0.0;
	public $Doors = 0;
	public $Color = '';
	public $Model = '';
	public $Brand = '';
	public $Year = 0;
	public $RegisterDate = '';
	public $Description = '';
	public $Address = '';
	public $Tickets = null; //Tickets
	
	/**
	* Using regex determines the type of ID given, either License Plate or RFID key
	*/
	public function init(){
		if(!empty($this->id)){
			//REGEX
			if(preg_match('/^[a-zA-Z0-9]{10}$/', $this->id) > 0){
				$this->RfidKey = $this->id;
			}
			elseif(preg_match('/^[a-zA-Z]{3}-[0-9]{3}$/', $this->id) > 0){
				$this->LicensePlate = $this->id;
			}
		}
		
		//Tickets are in toXML because it uses getVIN(). Only safe place to call it
	}

	protected function prepareLoadQuery(){
		if(!empty($this->LicensePlate) || !empty($this->RfidKey)){
			
			$sql = "SELECT
				V1.VIN,
				V1.Owner,
				V1.Expiration,
				V1.VehicleTag,
				V1.LoadCapacity,
				V1.HorsePower,
				V1.Doors,
				C1.Color,
				M2.Model,
				M2.Brand,
				V1.Year,
				V1.RegisterDate,
				V1.Description,
				V1.LicensePlate,
				V1.Address,
				V1.RfidKey
			FROM Vehicle V1
				INNER JOIN Colors C1 ON V1.Color = C1.idColors
				INNER JOIN (
					SELECT 
						M1.idModel, 
						M1.Model, 
						B1.Brand 
					FROM Model M1 
						INNER JOIN Brand B1 ON M1.Brand = B1.idBrand
					) M2 ON V1.Model = M2.idModel";
					
			if(!empty($this->LicensePlate)){
				$sql .= " WHERE V1.LicensePlate = :key";
			}
			elseif(!empty($this->RfidKey)){
				$sql .= " WHERE V1.RfidKey = :key";
			}

			$this->loadStmt = $this->conn->prepare($sql);
			
			if(!empty($this->LicensePlate)){
				$this->loadStmt->bindValue("key", $this->LicensePlate);
			}
			elseif(!empty($this->RfidKey)){
				$this->loadStmt->bindValue("key", $this->RfidKey);
			}
			
		}
	}
	
	public function prepareUpdateQuery(){
		
	}
	
	public function prepareCreateQuery(){

	}
	
	public function prepareDeleteQuery(){
		
	}
	
	public function toXML(\SimpleXMLElement &$xml=null){
		$return = false;
		if(is_null($xml)){
			$xml = new \SimpleXMLElement('<?xml version="1.0" encoding="utf-8" ?><xmlresponse></xmlresponse>');
			$return = true;
		}
		
		$vehicleXML = $xml->addChild('vehicle');
		
		$vehicleXML->addChild('licensePlate',$this->getLicensePlate());
		$vehicleXML->addChild('vin',$this->getVIN());
		$vehicleXML->addChild('rfidKey',$this->getRfidKey());
		$vehicleXML->addChild('owner',$this->getOwner());
		$vehicleXML->addChild('expiration',$this->getExpiration());
		$vehicleXML->addChild('vehicleTag',$this->getVehicleTag());
		$vehicleXML->addChild('loadCapacity',$this->getLoadCapacity());
		$vehicleXML->addChild('horsePower',$this->getHorsePower());
		$vehicleXML->addChild('doors',$this->getDoors());
		$vehicleXML->addChild('color',$this->getColor());
		$vehicleXML->addChild('model',$this->getModel());
		$vehicleXML->addChild('brand',$this->getBrand());
		$vehicleXML->addChild('year',$this->getYear());
		$vehicleXML->addChild('registerDate',$this->getRegisterDate());
		$vehicleXML->addChild('description',$this->getDescription());
		$vehicleXML->addChild('address',$this->getAddress());
		
		$this->Tickets = new Tickets($this->conn, $this->getVIN());
		$this->Tickets->setLoadCallback('loadTicketsVehicle','Ticket',$this->Tickets);
		
		$this->getTickets()->toXML($vehicleXML);
		
		if($return){		
			return $xml->asXML();
		}	
	}
}